

package org.ispider;
import java.util.ArrayList;

import org.ispider.fb.*;
import org.ispider.tweets.FetchTweetsJson;
import org.ispider.tweets.TweetInfo;
import org.springframework.dao.DataIntegrityViolationException

class SearchController {

    static allowedMethods = [create: ['GET', 'POST'], edit: ['GET', 'POST'], delete: 'POST']
    String qval;
    
    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [searchInstanceList: Search.list(params), searchInstanceTotal: Search.count()]
    }

    def create() {
		switch (request.method) {
		case 'GET':
        	[searchInstance: new Search(params)]
			break
		case 'POST':
	        def searchInstance = new Search(params)
	        if (!searchInstance.save(flush: true)) {
	            render view: 'create', model: [searchInstance: searchInstance]
	            return
	        }

			flash.message = message(code: 'default.created.message', args: [message(code: 'search.label', default: 'Search'), searchInstance.id])
	        redirect action: 'show', id: searchInstance.id
			break
		}
    }

    def show() {
        def searchInstance = Search.get(params.id)
        if (!searchInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'search.label', default: 'Search'), params.id])
            redirect action: 'list'
            return
        }

        [searchInstance: searchInstance]
    }

    def edit() {
		switch (request.method) {
		case 'GET':
	        def searchInstance = Search.get(params.id)
	        if (!searchInstance) {
	            flash.message = message(code: 'default.not.found.message', args: [message(code: 'search.label', default: 'Search'), params.id])
	            redirect action: 'list'
	            return
	        }

	        [searchInstance: searchInstance]
			break
		case 'POST':
	        def searchInstance = Search.get(params.id)
	        if (!searchInstance) {
	            flash.message = message(code: 'default.not.found.message', args: [message(code: 'search.label', default: 'Search'), params.id])
	            redirect action: 'list'
	            return
	        }

	        if (params.version) {
	            def version = params.version.toLong()
	            if (searchInstance.version > version) {
	                searchInstance.errors.rejectValue('version', 'default.optimistic.locking.failure',
	                          [message(code: 'search.label', default: 'Search')] as Object[],
	                          "Another user has updated this Search while you were editing")
	                render view: 'edit', model: [searchInstance: searchInstance]
	                return
	            }
	        }

	        searchInstance.properties = params

	        if (!searchInstance.save(flush: true)) {
	            render view: 'edit', model: [searchInstance: searchInstance]
	            return
	        }

			flash.message = message(code: 'default.updated.message', args: [message(code: 'search.label', default: 'Search'), searchInstance.id])
	        redirect action: 'show', id: searchInstance.id
			break
		}
    }

    def delete() {
        def searchInstance = Search.get(params.id)
        if (!searchInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'search.label', default: 'Search'), params.id])
            redirect action: 'list'
            return
        }

        try {
            searchInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'search.label', default: 'Search'), params.id])
            redirect action: 'list'
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'search.label', default: 'Search'), params.id])
            redirect action: 'show', id: params.id
        }
    }
	
	
	def social = {
	  //render "this if fb and twitter"	
		//render params.id
		ArrayList<FBInfo> fposts = new ArrayList<FBInfo>()
		ArrayList<TweetInfo> tposts = new ArrayList<TweetInfo>()
		if(params.id!=null){
		fposts = FetchFacebookJson.getFacebookData(params.id);
		tposts = FetchTweetsJson.getTwitterData(params.id);
		}
		[fposts : fposts, tposts : tposts,sval:params.id]
	}
	
	def searchSocial = {
	    
	
		if(params.socialq!=null)
		redirect(action: 'social', id: params.socialq) 
		else if(params.q!=null)
		redirect(action: 'social', id: params.q)
		else
		redirect(action: 'social', id:qval)
	}
	def review = {
		
	}
	
	def about = {
		
	}
	
	def deals = {
		
	}
	def index() {
		if(params.q==null)
		 params.q = qval
		qval = params.q
	    [qval:qval]
	}
	//render "this is search"
   // redirect action: 'search', params: params


}
